#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Skomart.Spider
// 
// Project : Jackie.Utility
// 
// File : ExpressionExtension.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 扩展Expression
// 
// History : 
//			2010-10-22 15:03:55	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

#endregion

namespace Jackie.Utility.CustomExtension
{
    /// <summary> 
    /// 扩展Expression
    /// </summary>
    public static class ExpressionExtension
    {
        public static Expression<Func<T,bool>> True<T>()
        {
            return f => true;
        }

        public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> thisExpression,
                                                                                            Expression<Func<T,bool>> expression)
        {
            var invokeExpression = Expression.Invoke(expression, thisExpression.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(thisExpression.Body, invokeExpression),
                                                    thisExpression.Parameters);
        }

        public static Expression<Func<T,bool>> OrElse<T>(this Expression<Func<T,bool>> thisExpression,Expression<Func<T,bool>> expression)
        {
            var invokeExpression = Expression.Invoke(expression, thisExpression.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.OrElse(thisExpression.Body, invokeExpression),
                                                    thisExpression.Parameters);
        }
    }
}